package edu.wtbu.cs.androiddemo.extra;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.annotation.OptIn;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.RawResourceDataSource;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;

import edu.wtbu.cs.androiddemo.R;


/**
 * 视频播放组件
 */
public class VideoSampleActivity extends AppCompatActivity {

    PlayerView playerView;
    ExoPlayer player;

    @OptIn(markerClass = UnstableApi.class)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_video_sample);
        playerView = findViewById(R.id.player_view);
        player = new ExoPlayer.Builder(VideoSampleActivity.this).build();
        playerView.setPlayer(player);
        RawResourceDataSource rawSource =new  RawResourceDataSource(VideoSampleActivity.this);
        DataSpec dataSpec= new DataSpec(RawResourceDataSource.buildRawResourceUri(R.raw.video4));
        try {
            rawSource.open(dataSpec);
        } catch (RawResourceDataSource.RawResourceDataSourceException e) {
            throw new RuntimeException(e);
        }
        MediaItem mediaItem = MediaItem.fromUri(rawSource.getUri());
        player.setMediaItem(mediaItem);
        player.prepare();
        player.play();
    }

    @Override
    protected void onStop() {
        super.onStop();
        player.release();
    }

//    @OptIn(markerClass = UnstableApi.class)
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if (!player.isReleased()){
//            player.release();
//        }
//    }
}